<template>
    <div class="index">
        <div class="header">
            首页
        </div>
        <div class="refresh">
            <v-refresh :on-refresh="onRefresh" :on-infinite="onInfinite" :noFlag="noFlag">
                <div class="swipercontainer">
                    <wc-swiper :duration="900" :interval="2000">
                        <wc-slide v-for="item in swiper">
                            <img :src="item" alt="">
                        </wc-slide>
                    </wc-swiper>
                </div>
                <div class="classify">
                    <ul class="classifycontainer">
                        <li class="classifyitem" v-for="item in classifyList.slice(0,4)">
                            <img :src="item.img" alt="">
                            <p>{{item.name}}</p>
                        </li>
                    </ul>
                    <ul class="classifycontainer">
                        <li class="classifyitem" v-for="item in classifyList.slice(4)">
                            <img :src="item.img" alt="">
                            <p>{{item.name}}</p>
                        </li>
                    </ul>
                </div>
                <p class="like">猜你喜欢</p>
                <ul>
                    <li class="article" v-for="(item,index) in listData">
                        <div class="articleinfo" @click="$router.push({name:'reviewProcess',query:{id:item._id }})">
                            <img class="cover" :src="item.cover.src" alt="">
                            <div class="desc">
                                <div class="articledesc">
                                    <div class="articletitle">{{item.title}}</div>
                                    <div class="articleintro">{{item.desc||'暂无简介哦~'}}</div>
                                    <div class="articlevalue">
                                        <p class="value">{{item.value||0}}元/单</p>
                                        <p class="tag">#{{item.tag||'无'}}</p>
                                    </div>
                                </div>
                                <div class="authordesc">
                                    <img :src="item.portrait" class="authorhead"></img>
                                    <p class="authorname">{{item.author}}</p>
                                </div>
                            </div>
                        </div>
                    </li>
                </ul>
            </v-refresh>
        </div>
        <navigation></navigation>
    </div>
</template>

<script>
    import navigation from '@/components/navigation';
    import vRefresh from '@/components/refresh';
    import calligraphy from './img/calligraphy.jpg';
    import cooking from './img/cooking.jpg';
    import draw from './img/draw.jpg';
    import handmade from './img/handmade.jpg';
    import language from './img/language.jpg';
    import pic from './img/pic.jpg';
    import other from './img/more.jpg';
    import lightlearning from './img/lightlearning.jpg';
    import index from './img/index_1.jpg';


    export default {
        name: 'index',
        data() {
            return {
                currentPage: 1, // 起始页
                itemsPerPage: 10, // 一页显示多少条
                pageStart: 0, // 开始页数
                pageEnd: 0, // 结束页数
                listData: [], // 数据存放数组
                noFlag: false,
                classifyList: [
                    {
                        name: '绘画',
                        router: 'draw',
                        img: draw
                    },
                    {
                        router: 'handmade',
                        name: '手工',
                        img: handmade
                    },
                    {
                        router: 'pic',
                        name: '照相',
                        img: pic
                    },
                    {
                        router: 'cooking',
                        name: '烹饪',
                        img: cooking
                    },
                    {
                        router: 'calligraphy',
                        name: '书法',
                        img: calligraphy
                    },
                    {
                        router: 'language',
                        name: '外语',
                        img: language
                    },
                    {
                        router: 'lightlearning',
                        name: '轻学习',
                        img: lightlearning
                    },
                    {
                        router: 'other',
                        name: '更多',
                        img: other
                    }
                ],
                swiper: [
                    index,
                    index,
                    index
                ]
            };
        },
        components: {
            navigation,
            vRefresh
        },
        created() {
        },
        mounted() {
            this.getList();
        },
        methods: {
            getList() {
                this.$Api.getProcessList({
                    currentPage: this.currentPage
                }).then((res) => {
                    if (res.data.count > 0) {
                        this.listData = res.data.data;
                    }
                }).catch((error) => {
                    console.log(error);
                });
            },
            onRefresh(done) {
                this.noFlag = false;
                this.currentPage = 1;
                this.getList();
                done(); // call done
            },
            onInfinite(done) {
                if (!this.noFlag) {
                    this.currentPage++;
                    const more = this.$el.querySelector('.load-more');
                    this.$Api.getArticleList({
                        currentPage: this.currentPage,
                        // eslint-disable-next-line
                        tagId: (this.tag && this.tag._id) || undefined
                    }).then((res) => {
                        if (res.data.data.length === 0) {
                            more.style.display = 'none'; // 隐藏加载条
                            // 走完数据调用方法
                            this.noFlag = true;
                        } else if (res.data.data.length < 10 && res.data.data.length !== 0) {
                            this.listData = this.listData.concat(res.data.data);
                            more.style.display = 'none'; // 隐藏加载条
                            // 走完数据调用方法
                            this.noFlag = true;
                        } else if (res.data.count === 10) {
                            this.listData.concat(res.data.data);
                        }
                    }).catch((error) => {
                        console.log(error);
                    });
                    done();
                }
            }
        },
        watch: {},
        computed: {}
    };
</script>

<style lang="scss">
    .index {
        .header {
            top: 0px;
            height: 44px;
            width: 100%;
            background: rgb(253, 163, 62);
            line-height: 44px;
            font-size: 17px;
            position: fixed;
            z-index: 999;
            color: white;
            font-weight: 300;
            text-align: center;
        }
        .swipercontainer {
            padding-bottom: 10px;
            background: whitesmoke;
            img {
                width: 100vw;
                height: 60vw;
            }
        }
        .classify {
            height: 200px;
            background: white;
            width: 100%;
            margin: 10px 0;
            .classifycontainer {
                display: flex;
                justify-content: space-around;
                .classifyitem {
                    width: 100px;
                    height: 100px;
                    padding: 20px;
                    display: inline-block;
                    font-size: 12px;
                    font-weight: 200;
                    color: #aaa;
                }
            }
        }
        .like {
            line-height: 20px;
            font-size: 15px;
            text-align: left;
            padding-left: 20px;
            color: #aaa;
        }
        .refresh {
            position: relative;
            height: 100vh;
            .yo-scroll {
                margin: 44px 0 55px;
                ul {
                    .article {
                        width: 100vw;
                        padding: 10px 20px;
                        .articleinfo {
                            border-radius: 8px 8px 8px 8px;
                            box-shadow: 0px 2px 8px #ccc;
                            .cover {
                                border-radius: 8px 8px 0 0;
                                width: 100%;
                            }
                            .desc {
                                border: .5px solid #eee;
                                padding: 20px 10px 10px 10px;
                                display: flex;
                                justify-content: space-around;
                                .articledesc {
                                    text-align: left;
                                    .articletitle {
                                        font-size: 17px;
                                        line-height: 40px;
                                        font-weight: 300;

                                    }
                                    .articleintro {
                                        font-weight: 300;

                                        font-size: 14px;
                                        color: #666;
                                    }
                                    .articlevalue {
                                        display: flex;
                                        justify-content: space-around;
                                        line-height: 40px;
                                        .value {
                                            color: orangered;
                                            font-weight: 200;
                                            font-size: 15px;
                                        }
                                        .tag {
                                            font-weight: 200;
                                            font-size: 15px;
                                            color: #aaa;
                                        }
                                    }
                                }
                                .authordesc {
                                    min-width: 70px;
                                    .authorhead {
                                        width: 50px;
                                        height: 50px;
                                        margin: 10px;
                                        border-radius: 50%;
                                    }
                                    .authorname {
                                        font-size: 13px;
                                        text-align: center;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

    }
</style>
